package com.business_clean.ui.adapter;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.SizeUtils;
import com.business_clean.R;
import com.business_clean.app.ext.CommonUtils;
import com.business_clean.data.initconfig.BaseStringNumEntity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.noober.background.drawable.DrawableCreator;

import org.jetbrains.annotations.NotNull;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class BaseStringNumAdapter extends BaseQuickAdapter<BaseStringNumEntity, BaseViewHolder> {

    private int selected = 0;// 0 是全选，传入数量就是限制

    private Set<Integer> selectedIndices = new HashSet<>();


    public BaseStringNumAdapter(int num) {
        super(R.layout.item_base_string_num_tag);
        this.selected = num;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, BaseStringNumEntity item) {
        TextView button = holder.getView(R.id.but_item_base);
        button.setText(item.getName());
        //数字
        TextView number = holder.getView(R.id.tv_item_base_num);
        number.setText(item.getNum());
        number.setBackground(setBackground(item.getBgColor()));
        number.setTextColor(Color.parseColor(item.getTextColor()));

        if (selectedIndices.contains(getItemPosition(item))) {
            button.setTextColor(ContextCompat.getColor(button.getContext(), R.color.base_primary));
            button.setBackground(CommonUtils.getBaseSelectedNoStrokeDrawable(button.getContext()));
        } else {
            button.setTextColor(ContextCompat.getColor(button.getContext(), R.color.base_primary_text_title));
            button.setBackground(CommonUtils.getBaseUnSelectedDrawable(button.getContext()));
        }
    }

    /**
     * 更新状态
     *
     * @param position
     */
    public void updateItem(int position) {
        if (0 == selected) {//区分是多选但是选择1个
            if (selectedIndices.contains(position)) {
                selectedIndices.remove(position);
            } else {
                selectedIndices.add(position);
            }
        } else {
            selectedIndices.clear();
            selectedIndices.add(position);
        }
        notifyDataSetChanged();
    }

    public Set<Integer> getSelectedIndices() {
        return selectedIndices;
    }

    /**
     * 获取逗号分割的String
     *
     * @return
     */
    public String getCommaSeparated() {
        StringBuilder stringBuilder = new StringBuilder();
        for (Integer index : selectedIndices) {
            stringBuilder.append(index);
            stringBuilder.append(",");
        }
        // 删除最后一个逗号
        if (stringBuilder.length() > 0) {
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        }
        return stringBuilder.toString();
    }

    /**
     * 下标+1
     *
     * @return
     */
    public String getCommaSeparated1() {
        StringBuilder stringBuilder = new StringBuilder();
        for (Integer index : selectedIndices) {
            stringBuilder.append(index + 1);
            stringBuilder.append(",");
        }
        // 删除最后一个逗号
        if (stringBuilder.length() > 0) {
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        }
        return stringBuilder.toString();
    }


    /**
     * 反选数据
     *
     * @param strings
     */
    public void invertData(List<String> strings) {
        selectedIndices.clear();
        for (int i = 0; i < strings.size(); i++) {
            selectedIndices.add(i);
        }
        notifyDataSetChanged();
    }


    private Drawable setBackground(String color) {
        Drawable drawable = new DrawableCreator.Builder().setCornersRadius(SizeUtils.dp2px(20))
                .setSolidColor(Color.parseColor(color))
                .build();
        return drawable;
    }
}
